package com.insta.adapterList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.insta.playdroid.R;
import com.insta.playdroid.metier.domain.Album;

public class ImageAlbumAdapter extends BaseAdapter {
	private Context mContext;
	private List<Album> albums = new ArrayList<Album>();
	private long largeurPixel;

	public ImageAlbumAdapter(Context c, List<Album> albums, long largeurPixel) {
		mContext = c;
		this.albums = albums;
		this.largeurPixel = largeurPixel;
	}

	public int getCount() {
		return albums.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {

		ImageView imageView;

		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		RelativeLayout gridView;

		gridView = new RelativeLayout(mContext);

		// get layout from albumitem.xml
		gridView = (RelativeLayout) inflater.inflate(R.layout.albumitem, null);
		gridView.setLayoutParams(new GridView.LayoutParams(
				(int) (largeurPixel / 2), LayoutParams.WRAP_CONTENT));

		// set value into textview
		TextView textView = (TextView) gridView.findViewById(R.id.itemalbum);
		textView.setText(albums.get(position).getTitre());


		// set image
		imageView = (ImageView) gridView.findViewById(R.id.imageAlbum);
		// imageView.setLayoutParams(new RelativeLayout.LayoutParams(120,
		// 120));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setPadding(8, 8, 8, 8);


		if (albums.get(position).getAlbumArt() != null) {
			Uri uri = Uri
					.fromFile(new File(albums.get(position).getAlbumArt()));
			imageView.setImageURI(uri);
		} else {
			imageView.setBackgroundResource(R.drawable.albumart);
		}

		return gridView;
	}

}
